package br.com.rookgraph.heuristica;

import br.com.rookgraph.Edge;
import br.com.rookgraph.Graph;

public class NotEstanque implements Heuristica { 

	@Override
	public Edge selectForCut(Edge candidate, Edge edgeCut) {

		Edge edgeSelect = edgeCut;

		//if (edgeSelect == null || candidate.alfa > edgeSelect.alfa) {
		//	edgeSelect = candidate;
		if (edgeSelect == null || Math.abs(candidate.beta) < Math.abs(edgeSelect.beta)) {
			edgeSelect = candidate;

		} else if (Math.abs(candidate.beta) == Math.abs(edgeSelect.beta)) {

			if (candidate.gama < edgeSelect.gama) {
				edgeSelect = candidate;

			}else if( candidate.gama == edgeSelect.gama){

				if( candidate.sigma < edgeSelect.sigma){
					edgeSelect = candidate;
				}		
			}
		}					
		return edgeSelect;
	}

	@Override
	public Edge selectForRep(Edge candidate, Edge edgeRep) {

		// Escolhe a aresta onde a troca for a Melhor, ou seja onde haverá a MAIOR perda de branches (o mais negativo)					
		if (edgeRep == null || Math.abs(candidate.beta) < Math.abs(edgeRep.beta)) {
			edgeRep = candidate;

		} else if (Math.abs(candidate.beta) == Math.abs(edgeRep.beta)) {
			
			// adicionada em 03/11
			//edgeRep = candidate;
			
			if (candidate.gama < edgeRep.gama) {
				edgeRep = candidate;

				}else if( candidate.gama == edgeRep.gama){

					if( candidate.sigma > edgeRep.sigma){
						edgeRep = candidate;
					}		
				}
			}
			
		return edgeRep;
	}

	/* (non-Javadoc)
	 * @see br.com.rookgraph.heuristica.Heuristica#isVantajoso(br.com.rookgraph.Edge, br.com.rookgraph.Edge)
	 *
	 * Esse metodo pode ser melhorado chamando um dos acima
	 * -TODO verifica isso
	 *
	 */
	@Override
	public boolean isVantajoso(Edge edgeRep, Edge edgeCut) {

		Edge vantajosa = selectForRep(edgeRep, edgeCut);

		return vantajosa == edgeRep;	
	}

	@Override
	public boolean melhorTroca(Graph tree, Edge edgeRep, Edge edgeCut, Edge edgeEntrada, Edge edgeSaida) {

		int difCan, difEnt;
		int edgeGama;

		//difCan =  tree.diffBranch(edgeRep, edgeCut);
		//difEnt =  tree.diffBranch(edgeEntrada, edgeSaida);
		if (tree.diffBranch(edgeRep, edgeCut) < tree.diffBranch(edgeEntrada, edgeSaida)){
			return true;

		}else if (tree.diffBranch(edgeRep, edgeCut) == tree.diffBranch(edgeEntrada, edgeSaida)) { 

			if (tree.lambda(edgeRep, edgeCut) < tree.lambda(edgeEntrada, edgeSaida)) {
				return  true;

			}else if (tree.lambda(edgeRep, edgeCut) == tree.lambda(edgeEntrada, edgeSaida)) { 

				if (tree.diffGama(edgeRep, edgeCut) < tree.diffGama(edgeEntrada, edgeSaida)) {
					return  true;
				}
			}
		}
		return false;
	}

	@Override
	public boolean ganhoTroca(Edge edgeEntrada, Edge edgeSaida) {
		// TODO Auto-generated method stub
		return false;
	}	

}
	


